/*
 * Copyright (C) 2009 CityLights Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.schizoidman.android.citylights.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;

/**
 * Created by IntelliJ IDEA.                     
 * User: xfeldman
 * Date: Sep 1, 2009
 * Time: 2:04:27 AM
 * To change this template use File | Settings | File Templates.
 */

public class TCBuildProxy extends TCEntityLoader {
	
	public static final int BUILD_SUCCESS = 0;
	public static final int BUILD_FAILURE = 1;
	public static final int BUILD_NEVER_RAN = 2;
	public static final int BUILD_UNKNOWN = 3;

	public static final String BUILD_STATUS = "build.status";
	public static final String BUILD_NUMBER = "build.number";
	public static final String ID = "build.id";
	public static final String HREF = "build.href";

	public static final String STATUS_TEXT = "build.statusText";
	public static final String START_DATE = "build.startDate";
	public static final String FINISH_DATE = "build.finishDate";
		
	private List<TCBuildProxy> mBuilds = new ArrayList<TCBuildProxy>();
    
	public TCBuildProxy(Intent intent) {
		super(intent);
        getData().setClassName("com.schizoidman.android.citylights", "com.schizoidman.android.citylights.BuildDetailsPage");
	}
	
	public TCBuildProxy (Intent sender, JSONObject jsObject) {
		this (sender);
		init (jsObject);
	}

	private void init(JSONObject jsObject) {
		Intent intent = getData();
		intent.putExtra(BUILD_STATUS, "SUCCESS".equals(jsObject.optString("@status")) ? BUILD_SUCCESS : BUILD_FAILURE );
		intent.putExtra(BUILD_NUMBER, jsObject.optString("@number"));
		intent.putExtra(ID, jsObject.optString("@id"));
		intent.putExtra(HREF, jsObject.optString("@href"));
		// updating Intent URI
		intent.setData(Uri.parse("http://" + intent.getData().getAuthority().concat(intent.getStringExtra(HREF))));
	}

	@Override
	public void loadFromJSON(String jsonString) throws JSONException
	{
		Log.d("CityLights", jsonString);
		Intent intent = getData();
		JSONObject jsBuild = new JSONObject(jsonString);
//		mStatusText = jsBuild.optString("statusText");
//		try {
//			mStartDate = DateFormat.getInstance().parse(jsBuild.optString("startDate"));
//			mFinishDate = mStartDate = DateFormat.getInstance().parse(jsBuild.optString("finishDate"));
//		} catch (ParseException e) {
//			Log.e("CityLights", e.getLocalizedMessage());
//		}
			
//		// ---  Workaround: When there's just one element, JSON returns one object instead of array
//		JSONObject jsBuild = root.optJSONObject("build");
//		if (null != jsBuild) {
//			TCBuildProxy proxy = new TCBuildProxy((JSONObject)jsBuild);
//			Log.i("CityLights", proxy.toString());
//			mBuilds.add (proxy);
//		}
//		// ---
//
//		JSONArray jsBuildsArray=root.optJSONArray("build");
//		if (null != jsBuildsArray) {
//			for(int i=0; i<jsBuildsArray.length(); i++)
//			{
//				TCBuildProxy proxy = new TCBuildProxy(jsBuildsArray.getJSONObject(i));
//				Log.i("CityLights", proxy.toString());
//				mBuilds.add (proxy);
//			}
//		}
	}

//	public String getStatusText() {
//		return mStatusText;
//	}
//
//	public Date getStartDate() {
//		return mStartDate;
//	}
//
//	public Date getFinishDate() {
//		return mFinishDate;
//	}
	
	@Override
	public String toString() {
		Intent intent = getData();
		return intent.getStringExtra(TCBuildTypeProxy.NAME) + ":" + intent.getStringExtra(BUILD_NUMBER) + 
		" = " + intent.getIntExtra(BUILD_STATUS, -2);
	}
	

}